package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSDAO;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSTransDAO;

public class UpdateDataBaseMonthDAO {
	private MyConnectDSDAO mcdsDAO = new MyConnectDSDAO();
	private MyConnectDSTransDAO mcdstDAO = new MyConnectDSTransDAO();
	private IBaseDAO baseDAO;
	private ITransactionDAO tranDAO;
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}
    
	/**
	 * 获取上月数据库解码信息
	 * @return
	 */
    public Map<String, Object> getLastMonthInfo(){
    	Map<String, Object> map = new HashMap<String,Object>();
    	String sql = "SELECT top 1 * FROM DataBaseInfo where DBInfo_Type = 4 and DBInfo_Eff<GETDATE() and DBInfo_Exp>GETDATE() order by DBInfo_DateMonth desc";
    	map = baseDAO.execQueryOne(sql, new Object[]{});
    	return map;
    }
    
    /**
     * 增加当前月份记录，与上月保持一致
     * @param lmInfo 上月信息
     * @param curTime 月库月份，格式：yyyy-mm-dd hh:MM:ss
     * @param monthName 月库月份，格式：yyyymm
     * @return
     */
    public boolean insertCurMonthInfo(Map<String, Object> lmInfo,String curTime,String monthName){
    	int num = 0;
    	String sql = "insert into DataBaseInfo (DBInfo_IP,DBInfo_Port,DBInfo_DBServerName,DBInfo_DBSLongName,DBInfo_UserName,DBInfo_Password,DBInfo_Type,DBInfo_DateMonth,DBInfo_DBName,DBInfo_Remark,DBInfo_OprtName,DBInfo_Eff,DBInfo_Exp) " +
    			" values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
    	try{
    		num = baseDAO.execInsert(sql, new Object[]{MapGetter.getString(lmInfo, "DBInfo_IP"),MapGetter.getString(lmInfo, "DBInfo_Port"),MapGetter.getString(lmInfo, "DBInfo_DBServerName")
    				,MapGetter.getString(lmInfo, "DBInfo_DBSLongName"),MapGetter.getString(lmInfo, "DBInfo_UserName"),MapGetter.getString(lmInfo, "DBInfo_Password"),MapGetter.getString(lmInfo, "DBInfo_Type")
        			,curTime,"CTY_Egn_B_"+monthName,"由定时任务自动更新",MapGetter.getString(lmInfo, "DBInfo_OprtName"),MapGetter.getString(lmInfo, "DBInfo_Eff")
        			,MapGetter.getString(lmInfo, "DBInfo_Exp")});
    	} catch(Exception e){
    		System.out.println("增加当月月库信息执行SQL异常!"+e);
    	}
    	if(num>0){
    		return true;
    	} else {
    		return false;
    	}
    	
    }
	/**
	 * 获取前四个月数据库解码信息
	 * @return
	 */
    public List<Map<String, Object>> getBmInfoList(){
    	List<Map<String, Object>> mapList = new ArrayList<Map<String,Object>>();
    	String sql = "SELECT top 4 * FROM DataBaseInfo where DBInfo_Type = 4 and DBInfo_Eff<GETDATE() and DBInfo_Exp>GETDATE() order by DBInfo_DateMonth desc";
    	mapList = baseDAO.execQuery(sql, new Object[]{});
    	return mapList;
    }
    
    /**
     * //更新第四月份信息
     * @param bmFiInfo 前第5月份信息
     * @param bmFoInfo 前第4月份信息
     * @return
     */
    public boolean updatebfMonthInfo(Map<String, Object> bmFiInfo,Map<String, Object> bmFoInfo){
    	int num = 0;
    	String sql = "update DataBaseInfo set DBInfo_IP = ?, DBInfo_Port = ?,DBInfo_DBServerName = ?,DBInfo_DBSLongName = ?,DBInfo_UserName = ?,DBInfo_Password = ?,DBInfo_Type = ?," +
    			"DBInfo_DateMonth = ?,DBInfo_DBName = ?,DBInfo_Remark = ?,DBInfo_OprtName = ?,DBInfo_Eff = ?,DBInfo_Exp = ? " +
    			" where DBInfo_ID = ? ";
    	try{
    		num = baseDAO.execUpdate(sql, new Object[]{MapGetter.getString(bmFiInfo, "DBInfo_IP"),MapGetter.getString(bmFiInfo, "DBInfo_Port"),MapGetter.getString(bmFiInfo, "DBInfo_DBServerName")
    				,MapGetter.getString(bmFiInfo, "DBInfo_DBSLongName"),MapGetter.getString(bmFiInfo, "DBInfo_UserName"),MapGetter.getString(bmFiInfo, "DBInfo_Password"),MapGetter.getString(bmFiInfo, "DBInfo_Type")
        			,MapGetter.getString(bmFoInfo, "DBInfo_DateMonth"),MapGetter.getString(bmFoInfo, "DBInfo_DBName"),"由定时任务自动更新",MapGetter.getString(bmFiInfo, "DBInfo_OprtName"),MapGetter.getString(bmFiInfo, "DBInfo_Eff")
        			,MapGetter.getString(bmFiInfo, "DBInfo_Exp"),MapGetter.getString(bmFoInfo, "DBInfo_ID")});
    	} catch(Exception e){
    		System.out.println("更新第四月份信息执行SQL异常!"+e);
    	}
    	if(num>0){
    		return true;
    	} else {
    		return false;
    	}
    	
    }
	

}